/* Copyright 2009 CtrlSpace Platform
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * Contributor(s):
 * 	Marc Obaldo (marcklaser@gmail.com)
 */

dojo.provide("ctrlspace.ui.UI");
dojo.require("ctrlspace.ui.tree.ProjectTree");
dojo.require("ctrlspace.ui.editor.EditorManager");
dojo.declare("ctrlspace.ui.UI", null, {
	// Attributes
	timeout: 0,

	// Constructor
	constructor : function(/* Object */args) {
		this.initialize();
	},

	// Methods
	initialize : function() {
		var bcOptions = {
			gutters : true,
			region : "center",
			design : "headline",
			id : "container"
		};
		
		this.menubar = null;

		this.container = new dijit.layout.BorderContainer(bcOptions,
				"container");

		this.editors = new ctrlspace.ui.editor.EditorManager( {
			tabStrip : true,
			region : "center",
			id : "editorManager",
			splitter : true,
			useMenu: true
		});

		this.tools = new dijit.layout.TabContainer( {
			tabStrip : true,
			style : "width: 250px;",
			region : "left",
			id : "toolTab",
			splitter : true
		});

        this.tree = new ctrlspace.ui.tree.ProjectTree({
            title: "Project Explorer",
            selected: true
        });

		this.container.addChild(this.editors);
		this.container.addChild(this.tools);

		var t2 = new dijit.layout.ContentPane({title: "Cloud"});
		var t3 = new dijit.layout.ContentPane({title: "SVN"});
		this.tools.addChild(t2);
		this.tools.addChild(t3);
        this.tools.addChild(this.tree);

		dijit.byId("main").addChild(this.container);
	},

	// Public methods
	getContainer: function() {
		return this.container;
	},
	
	setMenuBar: function(menubar) {
		if (this.menubar != null) {
			console.error('setMenuBar can only be called once!');
			return;
		}
		
		this.getContainer().addChild(menubar.menuContainer);
		this.menubar = menubar;
	},
	
	getMenuBar: function() {
		return this.menubar;
	},

	getEditors: function() {
		return this.editors;
	},

	getTools: function() {
		return this.tools;
	},
	
	getTree: function() {
		return this.tree;
	},

	newDialog : function() {
		// Create dialog
		var ws = new ctrlspace.ui.CreateProjectDialog();
		ws.show();
	},
	
	setStatus: function(message, level, timeout) {
		if (level == null) {
			level = ctrlspace.config.STATUS_INFO;
		}
		
		if (!(timeout == null || timeout == 0)) {
			window.clearTimeout(this.timeout);
			this.timeout = setTimeout(dojo.hitch(this, this.clearStatus), timeout * 1000);
		}
		
		if (level == ctrlspace.config.STATUS_PROGRESS) {
			dojo.byId("status").innerHTML = '<span class="' + level + '">' + message + '</span><span style="width: 32px; height: 32px;" class="csLoading">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>';
		} else {
			dojo.byId("status").innerHTML = '<span class="' + level + '">' + message + '</span>';
		}
	},
	
	clearStatus: function() {
		dojo.byId("status").innerHTML = '<span class="status_clear">&nbsp;</span>';
	},
	
	// Events
	projectCreated: function(project) {
		this.tree.refresh();
	}
});